﻿Imports System
Imports System.IO
Imports System.Text



Public Class ToolSettingForm

    '工具情報の宣言
    Public ToolID(100) As String               '工具ID
    Public ToolStyle(100) As Integer           '工具の種類（1:フェースミル/2:スクウェアエンドミル/3:ボールエンドミル/4:ドリル）
    Public ToolDiameter(100) As Double         '工具系
    Public CuttingLength(100) As Double        '有効刃長
    Public ToolLength(100) As Double           '工具長
    Public FlutesNum(100) As Integer           '刃数
    Public ToolMaterial(100) As Integer        '工具の素材
    Public Coating(100) As Integer             'コーティングの有無
    Public ShankDiameter(100) As Double

    Public SelectedToolID As String

    Dim IsUsableFaceMill As Boolean
    Dim IsUsableSquareEndMill As Boolean
    Dim IsUsableBallEndMill As Boolean
    Dim IsUsableDrill As Boolean
    Dim IsRestricting As Boolean
    Dim RestrictingLength As Double


    '工具の制限を取得
    Sub Restricting(ByVal FaceMill As Boolean, ByVal SquareEndMill As Boolean, ByVal BallEndMill As Boolean, ByVal Drill As Boolean, ByVal Restricting As Boolean, ByVal Length As Double)
        IsUsableFaceMill = FaceMill
        IsUsableSquareEndMill = SquareEndMill
        IsUsableBallEndMill = BallEndMill
        IsUsableDrill = Drill
        IsRestricting = Restricting
        RestrictingLength = Length
    End Sub

    '起動した時の関数（ToolInfo.txtから情報を読み込み）
    Private Sub ToolSettingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String = "ToolInfo.txt"
        Dim Reader As StreamReader = New StreamReader(path)
        Dim Read As String
        Dim retval(10) As String
        Dim ToolNum As Integer = 0
        Dim IsUsable As Boolean

        '工具情報の読み込み
        While Not Reader.Peek = -1
            IsUsable = False
            Read = Reader.ReadLine
            retval = Read.Split(CChar("/"))

            ToolID(ToolNum) = retval(0)
            ToolStyle(ToolNum) = CInt(retval(1))
            ToolDiameter(ToolNum) = CDbl(retval(2))
            CuttingLength(ToolNum) = CDbl(retval(3))
            ToolLength(ToolNum) = CDbl(retval(4))
            FlutesNum(ToolNum) = CInt(retval(5))
            ToolMaterial(ToolNum) = CInt(retval(6))
            Coating(ToolNum) = CInt(retval(7))
            ShankDiameter(ToolNum) = CDbl(retval(8))

            If ToolStyle(ToolNum) = 1 And IsUsableFaceMill Then IsUsable = True
            If ToolStyle(ToolNum) = 2 And IsUsableSquareEndMill Then IsUsable = True
            If ToolStyle(ToolNum) = 3 And IsUsableBallEndMill Then IsUsable = True
            If ToolStyle(ToolNum) = 4 And IsUsableDrill Then IsUsable = True


            If IsRestricting Then
                If ToolDiameter(ToolNum) > RestrictingLength Then
                    IsUsable = False
                End If
                If ToolStyle(ToolNum) = 4 And Not ToolDiameter(ToolNum) = RestrictingLength Then
                    IsUsable = False
                End If
            End If


            If IsUsable Then ToolNum += 1
        End While

        'Comboboxに工具の名前を表示
        With ComboBoxTool.Items
            .Clear()
            For i As Integer = 0 To ToolNum - 1
                .Add(ToolID(i))
            Next
        End With


    End Sub

    '適応ボタンを押した時の関数（ツールの情報を表示）
    Private Sub ButtonApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonApply.Click
        Dim Index As Integer = ComboBoxTool.SelectedIndex

        '工具情報を表示
        LabelID.Text = ToolID(Index)
        Select Case ToolStyle(Index)
            Case 1
                LabelStyle.Text = "フェースミル"
            Case 2
                LabelStyle.Text = "スクウェアエンドミル"
            Case 3
                LabelStyle.Text = "ボールエンドミル"
            Case 4
                LabelStyle.Text = "ドリル"
        End Select
        LabelDiameter.Text = CStr(ToolDiameter(Index))
        LabelCuttingLength.Text = CStr(CuttingLength(Index))
        LabelNumber.Text = CStr(FlutesNum(Index))
        LabelToolLength.Text = CStr(ToolLength(Index))
        Select Case ToolMaterial(Index)
            Case 1
                LabelToolMaterial.Text = "カーバイド"
            Case 2
                LabelToolMaterial.Text = "CBN"
            Case 3
                LabelToolMaterial.Text = "セラミック"
            Case 4
                LabelToolMaterial.Text = "HSS"
        End Select
        Select Case Coating(Index)
            Case 1
                LabelCoating.Text = "Coated"
            Case 2
                LabelCoating.Text = "UnCoated"
        End Select
        LabelShankDiameter.Text = CStr(ShankDiameter(Index))

    End Sub

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        SelectedToolID = LabelID.Text
        Close()
    End Sub

End Class